const UnionFind = require("../template")

function testUnionFindComponentAnalysis() {
  console.log(
    "1. 连通分量数量测试:",
    (() => {
      const uf = new UnionFind(5)
      
      // 初始状态：5个独立分量
      let componentCount = uf.getComponentCount()
      
      // 合并一些元素
      uf.union(0, 1)
      uf.union(2, 3)
      
      // 现在应该有3个分量：{0,1}, {2,3}, {4}
      componentCount = uf.getComponentCount()
      
      return componentCount === 3
    })()
  )

  console.log(
    "2. 最大连通分量大小测试:",
    (() => {
      const uf = new UnionFind(6)
      
      // 合并形成不同大小的分量
      uf.union(0, 1) // 大小2
      uf.union(2, 3) // 大小2
      uf.union(3, 4) // 大小3
      uf.union(4, 5) // 大小4
      
      const maxSize = uf.getMaxComponentSize()
      
      return maxSize === 4
    })()
  )

  console.log(
    "3. 连通分量大小分布测试:",
    (() => {
      const uf = new UnionFind(5)
      
      // 创建不同大小的分量
      uf.union(0, 1) // 大小2
      uf.union(2, 3) // 大小2
      // 4 独立，大小1
      
      const sizeDistribution = uf.getComponentSizeDistribution()
      
      return sizeDistribution[1] === 1 && // 1个大小为1的分量
             sizeDistribution[2] === 2 && // 2个大小为2的分量
             sizeDistribution[3] === 0    // 0个大小为3的分量
    })()
  )

  console.log(
    "4. 连通分量分析测试:",
    (() => {
      const uf = new UnionFind(4)
      
      // 创建连通分量
      uf.union(0, 1)
      uf.union(2, 3)
      
      const analysis = uf.analyzeComponents()
      
      return analysis.componentCount === 2 &&
             analysis.maxSize === 2 &&
             analysis.minSize === 2 &&
             analysis.totalElements === 4
    })()
  )

  console.log(
    "5. 连通分量遍历测试:",
    (() => {
      const uf = new UnionFind(5)
      
      // 创建连通分量
      uf.union(0, 1)
      uf.union(2, 3)
      uf.union(3, 4)
      
      const components = uf.getAllComponents()
      
      // 应该有两个分量：{0,1} 和 {2,3,4}
      return components.length === 2 &&
             components.some(comp => comp.includes(0) && comp.includes(1)) &&
             components.some(comp => comp.includes(2) && comp.includes(3) && comp.includes(4))
    })()
  )

  console.log(
    "6. 连通分量统计测试:",
    (() => {
      const uf = new UnionFind(6)
      
      // 创建连通分量
      uf.union(0, 1)
      uf.union(2, 3)
      uf.union(4, 5)
      uf.union(1, 2) // 合并两个分量
      
      const stats = uf.getComponentStats()
      
      return stats.componentCount === 2 && // {0,1,2,3} 和 {4,5}
             stats.maxSize === 4 &&
             stats.minSize === 2 &&
             stats.averageSize === 3
    })()
  )

  console.log(
    "7. 连通分量边界测试:",
    (() => {
      const uf = new UnionFind(1)
      
      // 只有一个元素
      const componentCount = uf.getComponentCount()
      const maxSize = uf.getMaxComponentSize()
      const components = uf.getAllComponents()
      
      return componentCount === 1 &&
             maxSize === 1 &&
             components.length === 1 &&
             components[0].includes(0)
    })()
  )

  console.log(
    "8. 连通分量性能测试:",
    (() => {
      const uf = new UnionFind(1000)
      
      // 创建大量连通分量
      for (let i = 0; i < 1000; i += 2) {
        uf.union(i, i + 1)
      }
      
      const start = performance.now()
      
      const componentCount = uf.getComponentCount()
      const maxSize = uf.getMaxComponentSize()
      const components = uf.getAllComponents()
      
      const end = performance.now()
      const duration = end - start
      
      return componentCount === 500 && // 500个大小为2的分量
             maxSize === 2 &&
             components.length === 500 &&
             duration < 100 // 应该在100ms内完成
    })()
  )

  console.log(
    "9. this上下文连通分量分析测试:",
    (() => {
      const analysisObj = {
        multiplier: 2,
        processAnalysis: function(size) {
          const uf = new UnionFind(size)
          for (let i = 0; i < size; i += 2) {
            uf.union(i, i + 1)
          }
          return uf.getComponentCount() * this.multiplier
        }
      }
      
      return analysisObj.processAnalysis(10) === 10 // 5*2=10
    })()
  )

  console.log(
    "10. 复杂对象this绑定连通分量分析测试:",
    (() => {
      const analysisConfigObj = {
        config: { threshold: 3 },
        processWithConfig: function(size) {
          const uf = new UnionFind(size)
          for (let i = 0; i < size; i += 2) {
            uf.union(i, i + 1)
          }
          return uf.getComponentCount() >= this.config.threshold
        }
      }
      
      return analysisConfigObj.processWithConfig(10) === true
    })()
  )

  console.log(
    "11. 连通分量一致性测试:",
    (() => {
      const uf = new UnionFind(6)
      
      // 创建连通分量
      uf.union(0, 1)
      uf.union(2, 3)
      uf.union(4, 5)
      
      // 多次分析验证一致性
      const count1 = uf.getComponentCount()
      const count2 = uf.getComponentCount()
      const maxSize1 = uf.getMaxComponentSize()
      const maxSize2 = uf.getMaxComponentSize()
      
      return count1 === count2 &&
             maxSize1 === maxSize2 &&
             count1 === 3 &&
             maxSize1 === 2
    })()
  )

  console.log(
    "12. 连通分量复杂测试:",
    (() => {
      const uf = new UnionFind(10)
      
      // 创建复杂的连通分量结构
      uf.union(0, 1)
      uf.union(1, 2)
      uf.union(3, 4)
      uf.union(5, 6)
      uf.union(6, 7)
      uf.union(8, 9)
      uf.union(2, 3) // 合并两个分量
      
      const analysis = uf.analyzeComponents()
      const components = uf.getAllComponents()
      
      return analysis.componentCount === 3 && // {0,1,2,3,4}, {5,6,7}, {8,9}
             analysis.maxSize === 5 &&
             analysis.minSize === 2 &&
             components.length === 3
    })()
  )
}

testUnionFindComponentAnalysis()
